<template>
<div>
    <nav-bar class="home-nav">
        <div slot="center">购物车</div>
    </nav-bar>
    <tab-control v-show="isTabFixed" class="fixed" ref="tabControlTop" :titles="['流行','新款','精选']" @itemClick="tabClick"></tab-control>
    <scroll class="content" ref="scroll" @scroll="contentScroll" @pullingUp="loadMore" :data="showGoodsList" :pull-up-load="true" :probe-type="3">
        <div>
            <home-swiper ref="hSwiper" :banners="banners"></home-swiper>
            <feature-view :features="recommends"></feature-view>
            <recommend-view></recommend-view>
            <tab-control :titles="['流行','新款','精选']" ref="tabControl" @itemClick="tabClick"></tab-control>
            <goods-list :goods="showGoodsList"></goods-list>
        </div>
    </scroll>
    <back-top class="back-top" @backTop="backTop" v-show="showBackTop">
        <!-- .native 需要监听组件原生事件时候，必须添加native修饰符才能监听 -->
        <!-- <back-top class="back-top" @click.native="backTop"> -->
        <img src="~assets/img/common/top.png" alt="">
    </back-top>
</div>
</template>

<script>
// 通用的控件
import NavBar from 'common/navbar/NavBar'
import Scroll from 'common/scroll/Scroll'

// 业务控件
import HomeSwiper from './childComps/HomeSwiper'
import FeatureView from './childComps/FeatureView'
import RecommendView from './childComps/RecommendView'
import TabControl from 'content/tabControl/TabControl'
import GoodsList from './childComps/GoodsList'
import BackTop from 'content/backTop/BackTop'
import {
    POP,
    NEW,
    SELL,
    BACKTOP_DISTANCE
} from '@/common/const'

import {
    getHomeMultidata,
    getHomeData,
    RECOMMEND,
    BANNER
} from 'network/home'

export default {
    name: 'home',
    components: {
        NavBar,
        HomeSwiper,
        FeatureView,
        RecommendView,
        TabControl,
        GoodsList,
        Scroll,
        BackTop
    },
    data() {
        return {
            banners: [],
            recommends: [],
            goodsList: {
                'pop': {
                    page: 1,
                    list: []
                },
                'new': {
                    page: 1,
                    list: []
                },
                'sell': {
                    page: 1,
                    list: []
                }
            },
            currentType: POP,
            showBackTop: false,
            isTabFixed: false,
            tabOffsetTop: 0,
        }
    },
    created() {
        console.log('创建Home')
        // 1.请求多个数据
        this.getMultiData()
        // 2.请求商品数据
        this.getHomeProducts(POP)
        this.getHomeProducts(NEW)
        this.getHomeProducts(SELL)
    },
    // updated() {
    //     this.tabOffsetTop = this.$refs.tabControl.$el.offsetTop
    //     console.log(this.tabOffsetTop);
    // },
    methods: {
        tabClick(type) {
            switch (type) {
                case 0:
                    this.currentType = POP
                    break
                case 1:
                    this.currentType = NEW
                    break
                case 2:
                    this.currentType = SELL
                    break
            }

            this.$refs.tabControlTop.currentIndex = type;
            this.$refs.tabControl.currentIndex = type;
        },
        contentScroll(position) {
            this.isTabFixed = position.y < -this.tabOffsetTop

            this.showBackTop = position.y < -BACKTOP_DISTANCE
        },
        loadMore() {
            this.getHomeProducts(this.currentType)
        },
        backTop() {
            this.$refs.scroll.scrollTo(0, 0, 300)
        },
        /**
         * 网络请求相关方法
         */
        getMultiData() {
            getHomeMultidata()
                .then((res) => {
                    this.banners = res.data[BANNER].list
                    this.recommends = res.data[RECOMMEND].list

                    // 下次更新DOM时,获取新的tabOffsetTop值(不保险,可以在updated钩子中获取)
                    this.$nextTick(() => {
                        this.tabOffsetTop = this.$refs.tabControl.$el.offsetTop
                    })
                })
        },
        getHomeProducts(type) {
            getHomeData(type, this.goodsList[type].page)
                .then((res) => {
                    const goodsList = res.data.list
                    this.goodsList[type].list.push(...goodsList)
                    this.goodsList[type].page += 1

                    this.$refs.scroll.finishPullUp()
                })
        },
    },
    computed: {
        showGoodsList() {
            return this.goodsList[this.currentType].list
        }
    },
    activated() {
        this.$refs.hSwiper.startTimer()
    },
    deactivated() {
        this.$refs.hSwiper.stopTimer()
    }
}
</script>

<style scoped>
.content {
    position: absolute;
    top: 44px;
    bottom: 49px;
    left: 0;
    right: 0;
}

.home-nav {
    background-color: var(--color-tint);
    font-weight: 700;
    color: white;
    /* border-bottom: 1px solid #eee;
    box-shadow: 0 1px 1px rgba(100, 100, 100, .08); */
}

/*
 cs3:
 calc(100%-97px) 计算属性计算数值
*/
.back-top {
    position: fixed;
    right: 10px;
    bottom: 60px;
}

.fixed {
    position: fixed;
    top: 44px;
    left: 0;
    right: 0;
}
</style>
